﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ChartDirector;

namespace GoodTrader
{
    public class GTGlobal
    {
        public static double mEndDate = 0.0;
        public static int MaxDownloadCount = 450;
        public static DateTime[] StockTimeToDateTimes(double[] aSTimes)
        {
            DateTime[] tTimes = new DateTime[aSTimes.Length];

            return tTimes;
        }

        public static int DateToInt()
        {
            DateTime curTime = DateTime.Now;
            string curDate = curTime.Year.ToString();
            if (curTime.Month < 10)
                curDate += ("0" + curTime.Month.ToString());
            else
                curDate += curTime.Month.ToString();
            if (curTime.Day < 10)
            {
                curDate += ("0" + curTime.Day.ToString());
            }
            else
            {
                curDate += curTime.Day.ToString();
            }

            return Convert.ToInt32(curDate);
        }

        public static void FindMinMax(double[] aSrc, ref double aMin, ref double aMax)
        {
            if (aSrc.Length == 0)
            {
                aMin = 0.0;
                aMax = 0.0;
                return;
            }
            aMin = aSrc[0];
            aMax = aSrc[0];
            for (int i = 0; i < aSrc.Length; i++)
            {
                if (aSrc[i] > aMax)
                    aMax = aSrc[i];
                if (aSrc[i] < aMin)
                    aMin = aSrc[i];
            }
        }

        public static void StockTimeToSysTime(double aSTime, ref int aHour, ref int aMin, ref int aSec)
        {
            string strTime = aSTime.ToString();
            int secondStart = 2;
            if (strTime.Length == 3)
                secondStart = 1;
            if (strTime == "0")
            {
                aHour = 0;
                aMin = 0;
                aSec = 0;
            }
            else
            {
                string hour = strTime.Substring(0, secondStart);
                string minute = strTime.Substring(secondStart, 2);
                aHour = int.Parse(hour);
                if (aHour > 23)
                    aHour = 23;
                aMin = int.Parse(minute);
                if (aMin > 59)
                    aMin = 59;
                aSec = 0;
            }
        }

        public static void ShiftArrayVal(double aNewVal, double[] aSrcArray, double[] aTempArray)
        {
            // 배열의 길이 값이 다르면 아무일도 하지 않는다.
            if (aSrcArray.Length != aTempArray.Length)
                return;
            // 원래 값을 임시 배열에 복사한다.
            Array.Copy(aSrcArray, aTempArray, aSrcArray.Length);
            // 맨 마지막 값을 채운다.
            aSrcArray[aSrcArray.Length - 1] = aNewVal;
            // 임시 배열의 값을 원래 배열에 복사한다.
            Array.Copy(aTempArray, 1, aSrcArray, 0, aTempArray.Length - 1);
        }

        public static void StockDateToSysDate(double aSDate, ref int aYear, ref int aMonth, ref int aDay)
        {
            string strDate = aSDate.ToString();
            if (strDate == "0")
            {
                aYear = 0;
                aMonth = 0;
                aDay = 0;
            }
            else
            {
                string year = strDate.Substring(0, 4);
                string month = strDate.Substring(4, 2);
                string day = strDate.Substring(6, 2);
                aYear = int.Parse(year);
                aMonth = int.Parse(month);
                aDay = int.Parse(day);
            }
        }

        public static void StockTimeToChartDirectorTime(double[] aSDate, double[] aSTimes, double[] aCTime)
        {
            for (int i = 0; i < aSTimes.Length; i++)
            {
                string strDate = aSDate[i].ToString();
                if (strDate == "0")
                {
                    break;
                }

                string year = strDate.Substring(0, 4);
                string month = strDate.Substring(4, 2);
                string day = strDate.Substring(6, 2);

                string strTime = aSTimes[i].ToString();
                int secondStart = 2;
                if (strTime.Length == 3)
                    secondStart = 1;
                if (strTime == "0")
                {
                    aCTime[i] = ChartDirector.Chart.chartTime(int.Parse(year), int.Parse(month), int.Parse(day), 0, 0, 0);
                }
                else
                {
                    string hour = strTime.Substring(0, secondStart);
                    string minute = strTime.Substring(secondStart, 2);
                    aCTime[i] = ChartDirector.Chart.chartTime(int.Parse(year), int.Parse(month), int.Parse(day), int.Parse(hour), int.Parse(minute), 0);
                }
            }
        }

        public static void StockTimeToChartDirectorTime(DateTime[] aSTimes, double[] aCTime)
        {
            if (aSTimes.Length != aCTime.Length)
                return;

            for (int i = 0; i < aSTimes.Length; i++)
            {
                aCTime[i] = ChartDirector.Chart.CTime(aSTimes[i]);
            }
        }
    }
}
